package com.biz.primus.ms.order.service.bulider;

import com.biz.primus.base.exception.BizSilentException;
import com.biz.primus.base.exception.utils.AssertUtils;
import com.biz.primus.model.depot.vo.DepotRespVo;
import com.biz.primus.model.oms.stream.ReturnCollectionChannel;
import com.biz.primus.model.order.exception.OrderException;
import com.biz.primus.model.order.exception.utils.AssertRequestParameter;
import com.biz.primus.model.order.vo.aftersale.ReturnOrderRequestVO;
import com.biz.primus.model.order.vo.aftersale.ReturnOrderResponseVO;
import com.biz.primus.ms.order.convertor.ReturnOrderConvertor;
import com.biz.primus.ms.order.dao.po.Order;
import com.biz.primus.ms.order.dao.po.ReturnOrder;
import com.biz.primus.ms.order.service.helper.OrderServiceHelper;
import com.biz.primus.ms.order.service.helper.ReturnDataStreamHelper;

/**
 * 退货单构造器
 *
 * @author Tiger Feng
 * @date 2017年11月20日
 */
public class CreateReturnOrderBuilder {
    /**
     * 退货单请求模型
     */
    private ReturnOrderRequestVO returnOrderRequestVO;
    /**
     * 订单服务助手，构造方法注入
     */
    private OrderServiceHelper serviceHelper;
    private ReturnDataStreamHelper dataStreamHelper;
    /**
     * 退货单响应模型
     */
    private ReturnOrderResponseVO returnOrderResponseVO;
    //-----------------------------------------
    // 临时变量
    /**
     * 订单
     */
    private Order order;
    /**
     * 门店
     */
    private DepotRespVo depotRespVo;
    /**
     * 退货单
     */
    private ReturnOrder returnOrder;

    private CreateReturnOrderBuilder() {
    }

    private CreateReturnOrderBuilder(ReturnOrderRequestVO returnOrderRequestVO,
                                     OrderServiceHelper serviceHelper,
                                     ReturnDataStreamHelper dataStreamHelper) {
        this.returnOrderRequestVO = returnOrderRequestVO;
        this.serviceHelper = serviceHelper;
        this.dataStreamHelper = dataStreamHelper;
    }

    /**
     * 1、创建构造器
     *
     * @param returnOrderRequestVO
     * @param serviceHelper
     * @return
     */
    public static CreateReturnOrderBuilder newBuilder(ReturnOrderRequestVO returnOrderRequestVO,
                                                      OrderServiceHelper serviceHelper,
                                                      ReturnDataStreamHelper dataStreamHelper) {
        AssertUtils.notNull(serviceHelper, OrderException.ORDER_HELPER_CAN_NOT_NULL);
        AssertUtils.notNull(dataStreamHelper, OrderException.ORDER_HELPER_CAN_NOT_NULL);
        AssertRequestParameter.notNull(returnOrderRequestVO, "退货订单请求模型不能为空");
        return new CreateReturnOrderBuilder(returnOrderRequestVO, serviceHelper, dataStreamHelper);
    }

    /**
     * 2、检查参数、准备关联数据
     *
     * @return
     */
    public CreateReturnOrderBuilder check() {
        AssertRequestParameter.notNull(returnOrderRequestVO.getOrderCode(), "订单编号不能为空");
        AssertRequestParameter.notNull(returnOrderRequestVO.getReturnType(), "退货(售后)类型不能为空");
        // 查询订单
        order = serviceHelper.getByOrderCode(returnOrderRequestVO.getOrderCode());
        AssertRequestParameter.notNull(order, "订单不存在");
        // 检查订单状态是否符合创建退货单
        // 只有 待评价 和 订单完成 的订单可以创建退货单
        switch (order.getState()) {
            case WAIT_PAY:
                throw new BizSilentException(OrderException.CANNOT_CREATE_RETURN_FOR_WAIT_PAY);
            case WAIT_SEND:
                throw new BizSilentException(OrderException.CANNOT_CREATE_RETURN_FOR_NOT_CONFIRM);
            case WAIT_RECEIVE:
                throw new BizSilentException(OrderException.CANNOT_CREATE_RETURN_FOR_NOT_CONFIRM);
            case WAIT_EVALUATION:
                // 待评价，通过
                break;
            case FINISHED:
                // 订单完成，通过
                break;
            case CANCELED:
                throw new BizSilentException(OrderException.CANNOT_CREATE_RETURN_FOR_CANCEL);
            case AFTER_SALE:
                throw new BizSilentException(OrderException.CANNOT_CREATE_RETURN_FOR_AFTER_SALE);
            case FINISHED_AFTER_SALE:
                throw new BizSilentException(OrderException.CANNOT_CREATE_RETURN_FOR_FINISHED_AFTER);
            default:
                throw new BizSilentException(OrderException.STATE_ERROR);
        }
        // 查询门店信息
        depotRespVo = serviceHelper.getDepotByCode(order.getDepotCode());
        AssertUtils.notNull(depotRespVo, OrderException.ORDER_EXCEPTION);
        // 商品项
        AssertRequestParameter.notEmpty(returnOrderRequestVO.getItems(), "商品项不能为空");
        return this;
    }

    /**
     * 3、创建退货单
     *
     * @return
     */
    public CreateReturnOrderBuilder create() {
        returnOrder = ReturnOrderConvertor.reqVO2PO(returnOrderRequestVO, depotRespVo);
        returnOrder = serviceHelper.saveReturnOrder(returnOrder);
        if (returnOrder == null) {
            throw new BizSilentException(OrderException.RETURN_ORDER_SAVE_FAILED);
        }
        return this;
    }

    /**
     * 4、推送到OMS中台
     *
     * @return
     */
    public CreateReturnOrderBuilder send2OMS() {
        ReturnCollectionChannel.ReturnData returnData = ReturnOrderConvertor.po2OmsReturnDataForFirstSend(returnOrder);
        dataStreamHelper.sendReturnOrder(returnData);
        return this;
    }

    /**
     * 3、获取退货单返回模型
     *
     * @return
     */
    public ReturnOrderResponseVO build() {
        // 退后单 转换成 退货单返回模型
        returnOrderResponseVO = ReturnOrderConvertor.po2RespVO(returnOrder);
        return returnOrderResponseVO;
    }

}
